#!/bin/sh -e

[ -f /etc/default/autogetty ] && . /etc/default/autogetty

[ 1 -gt $ENABLED ] && exit

while true; do
  for arg in $(cat /proc/cmdline); do
    case $arg in
      console=*)
        tty=${arg#console=}
        tty=${tty#/dev/}
        secure=`echo "${tty}" | cut -f1 -d,`
        secure=`/bin/egrep "^${secure}$" /etc/securetty || true`

        if [ -n "$tty" -a -n "${secure}" ]; then
            PORT=${tty%%,*}

            tmp=${tty##$PORT,}
            SPEED=${tmp%%n*}

            # if console=/dev/tty* without speed is given
            [ $SPEED = $PORT ] && SPEED=115200
            BITS=${tmp##${SPEED}n}

            # 8bit serial is default
            [ -z $BITS ] && BITS=8
            # [ 8 -eq $BITS ] && GETTY_ARGS="$GETTY_ARGS -8 "

            [ -z $SPEED ] && SPEED='115200,57600,38400,19200,9600'

            GETTY_ARGS="$AUTOGETTY_ARGS $GETTY_ARGS $SPEED $PORT"
            /sbin/getty $GETTY_ARGS
            unset GETTY_ARGS
        fi
    esac
  done
done
